package com.gsbelarus.gedemin.salary.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.text.InputFilter;
import android.text.Spanned;

import com.gsbelarus.gedemin.lib.sync.protocol.entity.SyncServiceTask;
import com.gsbelarus.gedemin.salary.R;
import com.gsbelarus.gedemin.salary.database.RealmHelper;
import com.gsbelarus.gedemin.salary.service.SyncService;

import io.realm.Realm;

public class PersonInfoPrefFragment extends PrefFragment {

    public static final String SURNAME = "surname_editText";
    public static final String FIRSTNAME = "firstname_editText";
    public static final String MIDDLENAME = "middlename_editText";
    public static final String PERSON_NUMBER = "person_number_editText";
    public static final String LIST_NUMBER = "list_number_editText";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.pref_auth);

        initEditSummary(findPreference(SURNAME),
                getResources().getString(R.string.not_specified_text))
                .setOnPreferenceClickListener(getClickListener());

        initEditSummary(findPreference(FIRSTNAME),
                getResources().getString(R.string.not_specified_text))
                .setOnPreferenceClickListener(getClickListener());

        initEditSummary(findPreference(MIDDLENAME),
                getResources().getString(R.string.not_specified_text))
                .setOnPreferenceClickListener(getClickListener());

        initEditSummary(findPreference(PERSON_NUMBER),
                getResources().getString(R.string.not_specified_text))
                .setOnPreferenceClickListener(getClickListener());

        initEditSummary(findPreference(LIST_NUMBER),
                getResources().getString(R.string.not_specified_text))
                .setOnPreferenceClickListener(getClickListener());
    }

    private Preference.OnPreferenceClickListener getClickListener() {
        return new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                InputFilter filter = new InputFilter() {
                    @Override
                    public CharSequence filter(CharSequence source, int start, int end,
                                               Spanned dest, int dstart, int dend) {
                        if (end > start) {

                            char[] acceptedChars = new char[]{'а', 'б', 'в', 'г', 'д', 'е', 'ё', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ы', 'ь', 'э', 'ю', 'я',
                                    'А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ё', 'Ж', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ъ', 'Ы', 'Ь', 'Э', 'Ю', 'Я', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};

                            for (int index = start; index < end; index++) {
                                if (!new String(acceptedChars).contains(String.valueOf(source.charAt(index)))) {
                                    return "";
                                }
                            }
                        }
                        return null;
                    }

                };
                showEditDialog(preference,
                        preference.getTitle().toString(),
                        getString(R.string.dialog_ok),
                        getString(R.string.cancel_text),
                        filter,
                        new OnPositiveListener() {
                            @Override
                            public void onPositive() {
                                putBoolean(SettingsFragment.WORK_MODE, SettingsFragment.isData(context));
                                RealmHelper realmHelper = new RealmHelper(Realm.getInstance(context));
                                realmHelper.clearDB(true);
                                realmHelper.getRealm().close();
                                context.startService(SyncService.getIntentWithTask(
                                        new Intent(context, SyncService.class),
                                        SyncServiceTask.TypeOfTask.NO_SYNC));
                            }
                        });
                return false;
            }
        };
    }
}